/*
 * @Author: DUST
 * @Date: 2023-06-12 17:44:33
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-17 16:32:41
 * @FilePath: \koa-node-ts\app\controller\UtilsController.ts
 * @Description:
 * undefined
 */

import { Context } from "koa";
import IpService from "../service/IpService";
import responese from "../../utils/responese";
import UtilsService from "../service/UtilsService";
import { isJSONStr } from "../../utils";

class UtilsController {
  /**
   * @description: 获取天气IP信息
   * @param {Context} ctx
   */
  async getLocationAndWeather(ctx: Context) {
    const ip = ctx.req.headers["public-ip"] as string;
    let ipInfo;
    if (ip) ipInfo = await IpService.getIPInfo(ip);
    console.log(ipInfo);

    return responese.success(ctx, ipInfo);
  }
  /**
   * @description: 获取各大站热搜信息
   * @param {Context} ctx
   */
  async getNewsList(ctx: Context) {
    const newsType = new URLSearchParams(ctx.querystring);
    if (isJSONStr(newsType.get("newsType") as string)) {
      const params: Array<string> = JSON.parse(
        newsType.get("newsType") as string
      );
      const data = await UtilsService.getHotNews(params);

      return responese.success(ctx, data);
    }
    responese.errorResponese(ctx, "参数类型错误");
  }
}

export default new UtilsController();
